#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Denoise micrograph  												#
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 26/03/2019                                             #
# Last Modification: 26/03/2019                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 40
#
# MANUAL: This script applies Warps "denoising" filter.
#
# PUBLICATION: Real-time cryo-EM data pre-processing with Warp: <A HREF="http://dx.doi.org/10.1101/338558">bioRxiv (2018) </A>
#
# DISPLAY: tempkeep
# DISPLAY: imagename
# DISPLAY: nonmaskimagename
# DISPLAY: imagenumber
# DISPLAY: imagesidelength
# DISPLAY: sample_pixel
# DISPLAY: raw_gaincorrectedstack
# DISPLAY: movie_stackname
# DISPLAY: movie_imagenumber_total
# DISPLAY: import_rawstack
# DISPLAY: KV
# DISPLAY: CS
# DISPLAY: defocus
# DISPLAY: phacon
# DISPLAY: denoise_S
# DISPLAY: denoise_F
# DISPLAY: denoise_lp_skip
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set imagenumber = ""
set imagesidelength = ""
set sample_pixel = ""
set raw_gaincorrectedstack = ""
set movie_stackname = ""
set movie_imagenumber_total = ""
set import_rawstack = ""
set KV = ""
set CS = ""
set defocus = ""
set phacon = ""
set denoise_S = ""
set denoise_F = ""
set denoise_lp_skip = ""
#
#$end_vars
#
set scriptname = process_denoise
\rm -f LOGS/${scriptname}.results
#

set ampcon = ` echo "scale=3; sqrt( 1 - ${phacon} * ${phacon} )" | bc `
# set def = `grep "set defocus =" 2dx_image.cfg | cut -d' ' -f4 | cut -d'"' -f2`
# set df1 = `echo ${defocus} | cut -d',' -f1`
# set df2 = `echo ${defocus} | cut -d',' -f2`
# set ast = `echo ${defocus} | cut -d',' -f3`
set df1 = `echo ${defocus} | sed 's/,/ /g' | awk '{s=$1} END {print s}'`
set df2 = `echo ${defocus} | sed 's/,/ /g' | awk '{s=$2} END {print s}'`
set ast = `echo ${defocus} | sed 's/,/ /g' | awk '{s=$3} END {print s}'`


if ( -e FASTDISK/${movie_stackname}.mrc ) then
	set input = "FASTDISK/${movie_stackname}.mrc"
else
	set input = "${movie_stackname}.mrc"
endif

set output = "${movie_stackname}"

if ( ${denoise_lp_skip} == "y" ) then
	set denoise_lp_skip = "--denoise_lp_skip"
else
	set denoise_lp_skip = ""
endif

echo "# IMAGE: ${movie_stackname}.mrc <Drift-corrected average image (2D, DoseWeighted)>" >> LOGS/${scriptname}.results

echo ": Now generating denoised micrograph:"
echo ": 	${app_python} ${proc_2dx}/focus.ctfapply ${input} --angpix ${sample_pixel} --df1 ${df1} --df2 ${df2} --ast ${ast} --cs ${CS} --kv ${KV} --ampcon ${ampcon} --denoise --denoise_S ${denoise_S} --denoise_F ${denoise_F} ${denoise_lp_skip}  --out ${movie_stackname}"
${app_python} ${proc_2dx}/focus.ctfapply ${input} --angpix ${sample_pixel} --df1 ${df1} --df2 ${df2} --ast ${ast} --cs ${CS} --kv ${KV} --ampcon ${ampcon} --denoise --denoise_S ${denoise_S} --denoise_F ${denoise_F} ${denoise_lp_skip} --out ${movie_stackname}

echo "# IMAGE-IMPORTANT: ${movie_stackname}_wf-denoised.mrc <Denoised micrograph>" >> LOGS/${scriptname}.results

${app_2dx_mrc_converter} --size 400 ${movie_stackname}_wf-denoised.mrc ${movie_stackname}_wf-denoised.png

##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
exit
#



